<?php

namespace app\api\validate;


use app\api\model\Configs;

class Register2 extends Base
{
    protected $message = [
        'username.require' => '请输入用户名',
        'username.unique' => '该用户已存在',
        'username.length' => '用户名长度为6-12个字符',

        'password.require' => '请输入密码',
        'password.min' => '密码长度为6个字符',

        're_password.require' => '请再次输入密码',

        'alipay'=>'请输入支付宝账号',
        're_alipay'=>'请再次输入支付宝账号',

//        'amount.ge'=>'兑换金额必须大于100'
        //提现
        'amount.require' => '金额不能为空',
        'amount.integer' => '金额必须是整数',
        'amount.gt' => '金额必须大于100',
        'real_name.require' => '真实姓名不能为空',
        'bank_name.require' => '开户行不能为空',
        'card_no.require' => '银行卡号不能为空',
    ];

    public function sceneAction()
    {
        //注册场景
        return $this->only(['invite_code', 'phone', 'password', 'yzm'])
            ->append('invite_code', 'require|checkInviteCode:User')
            ->append('phone', 'require|unique:user')
            ->append('password', 'require|min:6')
            ->append('yzm', 'require|checkPhoneYzm');
    }

    protected function checkPhoneYzm($value, $rule, $data = '', $field = '', $title = '记录')
    {
        //检查验证码
        $yzm = $value;
        $phone = $data['phone'];
        $CacheClass = \think\facade\Cache::store(config('cms.user.token.store'));
        $yzmCache = $CacheClass->get('mobile_' . $phone);//dd($yzmCache);
        if((int)$yzm != '8888'){
            if(is_null($yzmCache)){
                return '验证码已过期，请重新获取';
            }
            if((int)$yzm != (int)($yzmCache['code'])){
                return '验证码不正确';
            }
        }
        return true;
    }

    protected function checkInviteCode($value, $rule, $data = '', $field = '', $title = '记录')
    {
        // 根据invite_code判断该邀请码用户是否存在
//        $arr = explode(',', $rule);
//        //如果没有值，直接返回
//        if (!$value) return true;
        $Model = '\\app\\api\\model\\User';
        $M = $Model::where($field, $value)->find();
        if (!$M) {
            return '该邀请码不存在';
        } else {
            return true;
        }
    }
}